"use strict";
cc._RF.push(module, 'e70e9HZFR1Ie6sZpRYLLo6U', 'peopleArcherySelectRolePage');
// scripts/Game/Page/peopleArcherySelectRolePage.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var gameEngine_1 = require("../../engines/gameEngine");
var CsvFileKind_1 = require("../../engines/libs/CsvFileKind");
var PageBase_1 = require("../Base/PageBase");
var peopleArcheryRoleItem_1 = require("../Obj/peopleArcheryRoleItem");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var peopleArcherySelectRolePage = /** @class */ (function (_super) {
    __extends(peopleArcherySelectRolePage, _super);
    function peopleArcherySelectRolePage() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.roleItem = null;
        _this.pageView = null;
        _this.activeSkillNode = null;
        _this.passiveSkillNode = null;
        _this.leftNode = null;
        _this.rightNode = null;
        _this.btnUpgrade = null;
        _this.btnUnlock = null;
        _this.btnUse = null;
        _this.activeSkillRichText = null;
        _this.passiveSkillRichText = null;
        return _this;
    }
    peopleArcherySelectRolePage.prototype.OnPageOpen = function (prams) {
        return __awaiter(this, void 0, Promise, function () {
            return __generator(this, function (_a) {
                _super.prototype.OnPageOpen.call(this, prams);
                this.loadPage();
                return [2 /*return*/];
            });
        });
    };
    peopleArcherySelectRolePage.prototype.loadPage = function () {
        var ids = gameEngine_1.default.getManagers().csvDataManager.getCsvIds(CsvFileKind_1.CsvFileKind.CsvFile.peopleArcheryRole);
        for (var i = 0; i < ids.length; i++) {
            var id = ids[i];
            var data = gameEngine_1.default.getManagers().csvDataManager.getCsvDataItem(CsvFileKind_1.CsvFileKind.CsvFile.peopleArcheryRole, id);
            var item = cc.instantiate(this.roleItem);
            var is = item.getComponent(peopleArcheryRoleItem_1.default);
            item.active = true;
            is.bindData(data);
            this.pageView.addPage(item);
        }
        this.refrenshPage();
    };
    peopleArcherySelectRolePage.prototype.onBtnLeft = function () {
        var pageIndex = this.pageView.getCurrentPageIndex();
        if (pageIndex > 0) {
            // 可以左
            this.pageView.scrollToPage(pageIndex - 1, 0.2);
        }
    };
    peopleArcherySelectRolePage.prototype.onBtnRight = function () {
        var pageIndex = this.pageView.getCurrentPageIndex();
        if (pageIndex < this.pageView.getPages().length - 1) {
            // 可以右
            this.pageView.scrollToPage(pageIndex + 1, 0.2);
        }
    };
    peopleArcherySelectRolePage.prototype.refrenshPage = function () {
        this.leftNode.active = this.pageView.getCurrentPageIndex() > 0;
        this.rightNode.active = this.pageView.getCurrentPageIndex() < this.pageView.getPages().length - 1;
        this.refenshItem();
        this.refrenshTalent();
        gameEngine_1.default.emit("peopleArcheryRoleItem.refrensh");
    };
    peopleArcherySelectRolePage.prototype.refrenshTalent = function () {
        var curNode = this.pageView.getPages()[this.pageView.getCurrentPageIndex()];
        var cs = curNode.getComponent(peopleArcheryRoleItem_1.default);
        var cData = cs.data;
        var id = cData.role_id;
        var saveData = gameEngine_1.default.getManagers().peopleArcheryManager.getRoleData(id);
        // if(saveData){
        var level = (saveData === null || saveData === void 0 ? void 0 : saveData.level) || 1;
        var fileName = cData["role_file"];
        var filePath = "config/csv/peopleArchery/role/" + fileName;
        var data = gameEngine_1.default.getManagers().csvDataManager.getCsvDataItem(filePath, level);
        var active_Talent = data["active_Talent"];
        var activeTalent = gameEngine_1.default.getManagers().csvDataManager.getCsvDataItem(CsvFileKind_1.CsvFileKind.CsvFile.peopleArcheryTalent, active_Talent);
        var activeFile = activeTalent["talent_file"];
        var activeData = gameEngine_1.default.getManagers().csvDataManager.getCsvDataItem("config/csv/peopleArchery/talent/" + activeFile, level);
        var activeTypes = activeData["param_type"].split("|");
        var activeParams = activeData["param_num"].split("|");
        var active = { param_type: activeTypes[0], option_param: activeParams[0] };
        var activeOneText = gameEngine_1.default.getManagers().peopleArcheryManager.parseDataAsOptionText(active);
        var activeTwoText = gameEngine_1.default.getManagers().peopleArcheryManager.parseDataAsParamText(active);
        this.activeSkillRichText.string = "<color=#391102>" + activeOneText + "</c><color=#FF0000>" + activeTwoText + "</c>";
        var passive_Talent = data["passive_Talent"];
        var passiveTalent = gameEngine_1.default.getManagers().csvDataManager.getCsvDataItem(CsvFileKind_1.CsvFileKind.CsvFile.peopleArcheryTalent, passive_Talent);
        var passiveFile = passiveTalent["talent_file"];
        var passiveData = gameEngine_1.default.getManagers().csvDataManager.getCsvDataItem("config/csv/peopleArchery/talent/" + passiveFile, level);
        var passiveTypes = passiveData["param_type"].split("|");
        var passiveParams = passiveData["param_num"].split("|");
        var passive = { param_type: passiveTypes[0], option_param: passiveParams[0] };
        var passiveOneText = gameEngine_1.default.getManagers().peopleArcheryManager.parseDataAsOptionText(passive);
        var passiveTwoText = gameEngine_1.default.getManagers().peopleArcheryManager.parseDataAsParamText(passive);
        this.passiveSkillRichText.string = "<color=#391102>" + passiveOneText + "</c><color=#FF0000>" + passiveTwoText + "</c>";
        // }else{
        //     this.activeSkillRichText.string = `<color=#391102>需要解锁角色</c>`;
        //     this.passiveSkillRichText.string = `<color=#391102>需要解锁角色</c>`;
        // }
    };
    peopleArcherySelectRolePage.prototype.refenshItem = function () {
        var curNode = this.pageView.getPages()[this.pageView.getCurrentPageIndex()];
        var cs = curNode.getComponent(peopleArcheryRoleItem_1.default);
        var cData = cs.data;
        var id = cData.role_id;
        var saveData = gameEngine_1.default.getManagers().peopleArcheryManager.getRoleData(id);
        this.btnUnlock.active = saveData == null;
        this.btnUpgrade.active = saveData != null;
        this.btnUse.active = saveData != null;
        if (saveData) {
            var level = saveData.level;
            var fileName = cData["role_file"];
            var filePath = "config/csv/peopleArchery/role/" + fileName;
            var ids = gameEngine_1.default.getManagers().csvDataManager.getCsvIds(filePath);
            var maxNode = this.btnUpgrade.getChildByName("maxNode");
            var upgradeMoneyNode = this.btnUpgrade.getChildByName("moneyNode");
            var upgradeAdNode = this.btnUpgrade.getChildByName("adNode");
            maxNode.active = false;
            upgradeAdNode.active = false;
            upgradeMoneyNode.active = false;
            if (level >= ids.length) {
                // 等级已满
                maxNode.active = true;
            }
            else {
                // 获取当前等级的升级数据
                var param = gameEngine_1.default.getManagers().csvDataManager.getCsvDataItem(filePath, level);
                var type = param.upgrade_type;
                var num = param.upgrade_param;
                var lbNum = null;
                switch (type) {
                    case "0": {
                        lbNum = upgradeMoneyNode.getChildByName("layout").getChildByName("lbMoney").getComponent(cc.Label);
                        lbNum.string = "" + num;
                        upgradeMoneyNode.active = true;
                        break;
                    }
                    case "1": {
                        lbNum = upgradeAdNode.getChildByName("layout").getChildByName("lbAd").getComponent(cc.Label);
                        lbNum.string = "升级";
                        upgradeAdNode.active = true;
                        break;
                    }
                }
            }
        }
        else {
            var unlockType = cData["role_unlock_type"];
            var unlockParam = cData["role_unlock_param"];
            var unlockMoneyNode = this.btnUnlock.getChildByName("moneyNode");
            var unlockAdNode = this.btnUnlock.getChildByName("adNode");
            var lbUnlockMoney = unlockMoneyNode.getChildByName("layout").getChildByName("lbMoney").getComponent(cc.Label);
            var lbAdUnlock = unlockAdNode.getChildByName("layout").getChildByName("lbAd").getComponent(cc.Label);
            unlockAdNode.active = false;
            unlockMoneyNode.active = false;
            switch (unlockType) {
                case "1": {
                    lbUnlockMoney.string = "" + unlockParam;
                    unlockMoneyNode.active = true;
                    break;
                }
                case "2": {
                    lbAdUnlock.string = (gameEngine_1.default.getManagers().peopleArcheryManager.getData().adNum[cData["role_id"]] || 0) + "/" + unlockParam;
                    unlockAdNode.active = true;
                    break;
                }
            }
        }
        var useLb = this.btnUse.getChildByName("lbUse").getComponent(cc.Label);
        useLb.string = cData.role_id == gameEngine_1.default.getManagers().peopleArcheryManager.getData().curUseRoleId ? "已出战" : "出战";
    };
    peopleArcherySelectRolePage.prototype.onBtnUpGrade = function () {
        var _this = this;
        // 升级
        gameEngine_1.default.getManagers().soundMgr.playSound("mainSound/anNiu");
        var curNode = this.pageView.getPages()[this.pageView.getCurrentPageIndex()];
        var cs = curNode.getComponent(peopleArcheryRoleItem_1.default);
        var cData = cs.data;
        var id = cData.role_id;
        var saveData = gameEngine_1.default.getManagers().peopleArcheryManager.getRoleData(id);
        if (!saveData) {
            gameEngine_1.default.getModules().uiControlModule.showMessageBox("请先解锁");
            return;
        }
        var fileName = cData["role_file"];
        var filePath = "config/csv/peopleArchery/role/" + fileName;
        var level = saveData.level;
        var ids = gameEngine_1.default.getManagers().csvDataManager.getCsvIds(filePath);
        if (level >= ids.length) {
            // 等级已满
            gameEngine_1.default.getModules().uiControlModule.showMessageBox("等级已满");
            return;
        }
        var d = gameEngine_1.default.getManagers().csvDataManager.getCsvDataItem(filePath, saveData.level);
        var upgrade = d["upgrade_type"];
        switch (upgrade) {
            case "0": {
                var money = Number(d["upgrade_param"]);
                if (gameEngine_1.default.getManagers().peopleArcheryManager.getData().money >= money) {
                    gameEngine_1.default.getManagers().peopleArcheryManager.setData({ money: -money }, true);
                    this.onUpGrade();
                }
                else {
                    gameEngine_1.default.getModules().uiControlModule.showMessageBox("修为不够");
                }
                break;
            }
            case "1": {
                if (gameEngine_1.default.getServices().userSrv.getCoins() > 0) {
                    this.onUpGrade();
                    gameEngine_1.default.getServices().userSrv.setCoins(-1, true);
                }
                else {
                    gameEngine_1.default.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(function () {
                        _this.onUpGrade();
                    }, function () {
                    }, null, "箭箭箭看广告升级人物");
                }
                break;
            }
        }
    };
    peopleArcherySelectRolePage.prototype.onBtnUnlock = function () {
        var _this = this;
        // 解锁
        gameEngine_1.default.getManagers().soundMgr.playSound("mainSound/anNiu");
        var curNode = this.pageView.getPages()[this.pageView.getCurrentPageIndex()];
        var cs = curNode.getComponent(peopleArcheryRoleItem_1.default);
        var cData = cs.data;
        var id = cData.role_id;
        var saveData = gameEngine_1.default.getManagers().peopleArcheryManager.getRoleData(id);
        if (saveData) {
            gameEngine_1.default.getModules().uiControlModule.showMessageBox("已经解锁");
            return;
        }
        var type = cData["role_unlock_type"];
        var param = Number(cData["role_unlock_param"]);
        switch (type) {
            case "1": {
                if (gameEngine_1.default.getManagers().peopleArcheryManager.getData().money >= param) {
                    gameEngine_1.default.getManagers().peopleArcheryManager.setData({ money: -param }, true);
                    this.onUnlock();
                }
                else {
                    gameEngine_1.default.getModules().uiControlModule.showMessageBox("钱不够");
                }
                break;
            }
            case "2": {
                if (gameEngine_1.default.getServices().userSrv.getCoins() > 0) {
                    var videoCount = gameEngine_1.default.getManagers().peopleArcheryManager.getData().adNum[id];
                    if (videoCount == null) {
                        gameEngine_1.default.getManagers().peopleArcheryManager.getData().adNum[id] = 0;
                        videoCount = 0;
                    }
                    videoCount++;
                    gameEngine_1.default.getManagers().peopleArcheryManager.getData().adNum[id] = videoCount;
                    gameEngine_1.default.getManagers().peopleArcheryManager.writeData();
                    if (videoCount >= param) {
                        this.onUnlock();
                    }
                    gameEngine_1.default.getServices().userSrv.setCoins(-1, true);
                    this.refenshItem();
                }
                else {
                    gameEngine_1.default.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(function () {
                        var videoCount = gameEngine_1.default.getManagers().peopleArcheryManager.getData().adNum[id];
                        if (videoCount == null) {
                            gameEngine_1.default.getManagers().peopleArcheryManager.getData().adNum[id] = 0;
                            videoCount = 0;
                        }
                        videoCount++;
                        gameEngine_1.default.getManagers().peopleArcheryManager.getData().adNum[id] = videoCount;
                        gameEngine_1.default.getManagers().peopleArcheryManager.writeData();
                        if (videoCount >= param) {
                            _this.onUnlock();
                        }
                        _this.refenshItem();
                    }, function () {
                    }, null, "箭箭箭看广告解锁人物");
                }
                break;
            }
        }
    };
    peopleArcherySelectRolePage.prototype.onBtnUse = function () {
        gameEngine_1.default.getManagers().soundMgr.playSound("mainSound/anNiu");
        this.onUse();
    };
    peopleArcherySelectRolePage.prototype.onUse = function () {
        var curNode = this.pageView.getPages()[this.pageView.getCurrentPageIndex()];
        var cs = curNode.getComponent(peopleArcheryRoleItem_1.default);
        var cData = cs.data;
        var id = cData.role_id;
        gameEngine_1.default.getManagers().peopleArcheryManager.setData({ curUseRoleId: id }, false);
        this.refrenshPage();
        gameEngine_1.default.emit("peopleArcheryStartPage.refrenshRole");
    };
    peopleArcherySelectRolePage.prototype.onUpGrade = function () {
        var curNode = this.pageView.getPages()[this.pageView.getCurrentPageIndex()];
        var cs = curNode.getComponent(peopleArcheryRoleItem_1.default);
        var cData = cs.data;
        var id = cData.role_id;
        var saveData = gameEngine_1.default.getManagers().peopleArcheryManager.getRoleData(id);
        saveData.level++;
        this.refrenshPage();
        gameEngine_1.default.getManagers().peopleArcheryManager.writeData();
    };
    peopleArcherySelectRolePage.prototype.onUnlock = function () {
        var curNode = this.pageView.getPages()[this.pageView.getCurrentPageIndex()];
        var cs = curNode.getComponent(peopleArcheryRoleItem_1.default);
        var cData = cs.data;
        var id = cData.role_id;
        gameEngine_1.default.getManagers().peopleArcheryManager.addOneRole(id);
        this.refrenshPage();
    };
    __decorate([
        property({ type: cc.Node, tooltip: "角色item" })
    ], peopleArcherySelectRolePage.prototype, "roleItem", void 0);
    __decorate([
        property({ type: cc.PageView, tooltip: "角色content" })
    ], peopleArcherySelectRolePage.prototype, "pageView", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "主动技能属性节点" })
    ], peopleArcherySelectRolePage.prototype, "activeSkillNode", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "被动技能属性节点" })
    ], peopleArcherySelectRolePage.prototype, "passiveSkillNode", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "左边节点" })
    ], peopleArcherySelectRolePage.prototype, "leftNode", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "右边节点" })
    ], peopleArcherySelectRolePage.prototype, "rightNode", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "升级按钮" })
    ], peopleArcherySelectRolePage.prototype, "btnUpgrade", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "解锁按钮" })
    ], peopleArcherySelectRolePage.prototype, "btnUnlock", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "使用按钮" })
    ], peopleArcherySelectRolePage.prototype, "btnUse", void 0);
    __decorate([
        property({ type: cc.RichText, tooltip: "主动技能文本" })
    ], peopleArcherySelectRolePage.prototype, "activeSkillRichText", void 0);
    __decorate([
        property({ type: cc.RichText, tooltip: "被动技能文本" })
    ], peopleArcherySelectRolePage.prototype, "passiveSkillRichText", void 0);
    peopleArcherySelectRolePage = __decorate([
        ccclass
    ], peopleArcherySelectRolePage);
    return peopleArcherySelectRolePage;
}(PageBase_1.default));
exports.default = peopleArcherySelectRolePage;

cc._RF.pop();